package com.finance.action.admin.manage;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.JsonVo;
import com.common.vo.PageVo;
import com.finance.entity.User;
import com.finance.service.UserService;
import com.web.utils.DateUtil;
import com.web.utils.UserManagerUtil;

@Controller("adminManageAction")
@RequestMapping("admin/manage")
public class ManageAction {

	@Resource(name = "UserService")
	private UserService userService;

	/*
	 * /*************************************************************************
	 * *********************用户
	 * ****************************************************
	 * *****************************************
	 */

	/**
	 * 
	 * 用户列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("manage_user_list")
	public String manage_user_list(Model model) {
		User user = UserManagerUtil.getCurrentUser();
		if (user.getJurisdiction() == 3) {
			return "redirect:role.htm";
		}
		model.addAttribute("user", user);
		return "manage/manage/user_list";
	}

	@RequestMapping("role.htm")
	public String role() {
		return "manage/role";
	}

	@ResponseBody
	@RequestMapping("ajax_manage_user_list")
	public Map<String, Object> ajax_manage_user_list(
			@RequestParam(defaultValue = "1") int pageNum, User search)
			throws UnsupportedEncodingException {

		if (StringUtils.isNotEmpty(search.getName())) {
			String name = new String(search.getName().getBytes("iso-8859-1"),
					"UTF-8");
			search.setName(name);
		}

		if (StringUtils.isNotEmpty(search.getPhone())) {
			String phone = new String(search.getPhone().getBytes("iso-8859-1"),
					"UTF-8");
			search.setPhone(phone);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		PageVo<User> page = userService.findPageBySearch(search, pageNum);
		for (int i = 0; i < page.getList().size(); i++) {
			Date time = page.getList().get(i).getCreationTime();
			page.getList().get(i).setCreateTime(DateUtil.Date2Stirng(time));
		}
		map.put("code", 0);
		map.put("msg", 0);
		map.put("count", page.getCount());
		map.put("data", page.getList());
		return map;
	}

	/*
	 * 用户基本信息
	 */
	@RequestMapping("user_basic.htm")
	public String user_basic(Model model) {
		User user = UserManagerUtil.getCurrentUser();
		model.addAttribute("user", user);
		return "manage/manage/user_basic";
	}

	/*
	 * 修改用户密码
	 */
	@RequestMapping("edit_pd.htm")
	public String edit_pd() {
		return "manage/manage/edit_pd";
	}

	@ResponseBody
	@RequestMapping("ajax_user_edit_pd")
	public JsonVo<String> edit_pd(String oldPd, String newPd, String newPd1,
			User record) {
		JsonVo<String> json = new JsonVo<String>();
		User user = UserManagerUtil.getCurrentUser();
		if (user == null) {
			json.setSuccess(false);
			json.setMsg("登陆超时，请重新登陆");
			json.setCode(400);
		} else {
			if (StringUtils.equals(DigestUtils.md5Hex(oldPd),
					user.getPassword())) {
				if (StringUtils.equals(newPd, newPd1)) {
					record.setPassword(newPd1);
					userService.updateByPrimaryKeySelective(record);
					json.setMsg("成功修改密码");
					json.setSuccess(true);
					json.setCode(200);
				} else {
					json.setSuccess(false);
					json.setMsg("两次密码不相同，请重新输入");
					json.setCode(401);
				}
			} else {
				json.setCode(402);
				json.setSuccess(false);
				json.setMsg("原始密码输入有误，请重新输入");
			}
		}
		return json;
	}

	/**
	 * 修改状态
	 */

	@ResponseBody
	@RequestMapping("ajax_manage_update_user_state")
	public JsonVo<String> ajax_manage_update_user_state(User record) {
		JsonVo<String> json = new JsonVo<String>();
		User user = UserManagerUtil.getCurrentUser();
		if (StringUtils.equals(user.getUserId(), record.getUserId())) {
			json.setSuccess(false);
			json.setMsg("不可对自己进行操作");
		} else {
			if (record.getState() == 2) {
				userService.updateByPrimaryKeySelective(record);
				json.setCode(200);
				json.setSuccess(true);
				json.setMsg("已对" + record.getRole() + ":" + record.getName()
						+ "停用");
			} else {
				userService.updateByPrimaryKeySelective(record);
				json.setCode(200);
				json.setSuccess(true);
				json.setMsg("已对" + record.getRole() + ":" + record.getName()
						+ "启用");
			}
		}
		return json;
	}

	/**
	 * 添加用户
	 */
	@RequestMapping("manage_user_add")
	public String manage_user_add() {
		return "manage/manage/user_add";
	}

	@ResponseBody
	@RequestMapping("ajax_manage_user_add")
	public JsonVo<String> ajax_manage_user_add(User record) {
		JsonVo<String> json = new JsonVo<String>();
		record.setPassword(DigestUtils.md5Hex("123456"));
		record.setCreationTime(new Date());
		String userId = DateUtil
				.toStringXiumaDateTime(record.getCreationTime());
		record.setUserId(userId);
		userService.insert(record);
		json.setCode(200);
		json.setMsg("已成功添加用户" + record.getName());
		json.setSuccess(true);
		return json;
	}

	
	@RequestMapping("user_edit.htm")
	public String user_edit(String userId,Model model) {
		User user = userService.selectByPrimaryKey(userId);
		model.addAttribute("user", user);
		return "manage/manage/user_edit";
	}
	
	@ResponseBody
	@RequestMapping("ajax_manage_user_edit")
	public JsonVo<String> ajax_manage_user_edit(User record) {
		JsonVo<String> json = new JsonVo<String>();
		record.setPassword(DigestUtils.md5Hex(record.getPassword()));
		record.setCreationTime(new Date());
		userService.updateByPrimaryKeySelective(record);
		json.setCode(200);
		json.setMsg("已成功修改用户" + record.getName());
		json.setSuccess(true);
		return json;
	}
	
}
